package org.example.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCLogin2 {
    public static void main(String[] args) {
        try ( Connection connection = DBUtil.getConnection()){
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名：");
            String username = scanner.nextLine();
            System.out.println("请输入密码：");
            String password = scanner.nextLine();
            //1.创建预编译SQL的执行对象;
            PreparedStatement preparedStatement = connection.prepareStatement("select nickname from user where username = ? and password = ?");
            //2.设置占位符?的值;
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            //3.执行SQL语句;
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                System.out.println("恭喜你，" + resultSet.getString("nickname")+ "登录成功");
            }else {
                System.out.println("用户名或密码错误");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
